﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Business.Mst;
using ESOL.Business.Chk;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.WinUI.Manufacture;

namespace ESOL.WinUI.Qc
{
    public partial class frmManageIPQC : BaseForm.frmBaseMdiBusinessForm
    {
        private QcIpqcOrderEntity mSelectedIpqcOrder;

        public frmManageIPQC()
        {
            InitializeComponent();
        }

        #region Functions
        private void BindComobox()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value", typeof(int));
            dt.Rows.Add("", null);
            dt.Rows.Add("通过", 0);
            dt.Rows.Add("不良", 1);
            cmbResultTag.DataSource = dt;
            cmbResultTag.DisplayMember = "name";
            cmbResultTag.ValueMember = "value";

            repositoryItemGridLookUpEdit1.DataSource = dt;
            repositoryItemGridLookUpEdit1.DisplayMember = "name";
            repositoryItemGridLookUpEdit1.ValueMember = "value";

            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            DataTable dt1 = MstCommonItemBs.GetMstCommonItemDtByType("STATUS");
            repositoryItemGridLookUpEdit2.DataSource = dt1;
            repositoryItemGridLookUpEdit2.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit2.ValueMember = "ITEM_CODE";

        }
        private void SetField()
        {
            cmbResultTag.SetSelectedValue(1);
        }
        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtIqcOrder.Text) == false)
            {
                sCondition += "and qio.ORDER_CODE like '" + txtIqcOrder.Text + "' ";
            }
            if (txtWorkingOrder.Tag != null)
            {
                sCondition += "and qio.ORDER_ITEM_CODE like '%" + ((ProdPlanOrderEntity)txtWorkingOrder.Tag).PlanOrderId + "%' ";
            }
            if (string.IsNullOrEmpty(cmbResultTag.SelectedValue.ToString()) == false)
            {
                sCondition += "and qio.RESULT_TAG='" + cmbResultTag.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                sCondition += "and qio.STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            return sCondition;
        }
        private void BindGrid()
        {
            string sCondition = GetCondition();
            string sOrderBy = "order by qio.ORDER_CODE ";
            DataTable dt = QcIpqcOrderBs.GetDtAllByCondition(sCondition, "", sOrderBy);
            gvMain.DataSource = dt;
        }
        private void SetButton()
        {
            if (mSelectedIpqcOrder == null)
                return;
            switch (mSelectedIpqcOrder.Status)
            {
                case "STATUS01":
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS02":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = true;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS03":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = true;
                    break;
                case "STATUS04":
                    btnModify.Enabled = true;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS05":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS06":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
            }
        }
        #endregion

        #region Events
        private void frmIPQCManage_Load(object sender, EventArgs e)
        {
            BindComobox();
            SetField();
            BindGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            mSelectedIpqcOrder = new QcIpqcOrderEntity(dt.Rows[e.RowHandle]);
            SetButton();
        }

        protected override void Click_View()
        {
            if (mSelectedIpqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIPQC frm = new frmMaintainIPQC(mSelectedIpqcOrder);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainIPQC frm = new frmMaintainIPQC();
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedIpqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIPQC frm = new frmMaintainIPQC(mSelectedIpqcOrder);
            frm.ThisActionType = ActionType.Modify;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
                mSelectedIpqcOrder = null;
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedIpqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确定要删除质检单？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                mSelectedIpqcOrder.Status = "STATUS05";
                if (QcIpqcOrderBs.DeleteQcIpqcOrder(mSelectedIpqcOrder))
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGrid();
                    mSelectedIpqcOrder = null;
                }
            }
        }
        protected override void Click_Submit()
        {
            if (mSelectedIpqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确定要送审？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                mSelectedIpqcOrder.Status = "STATUS02";
                if (QcIpqcOrderBs.UpdateQcIpqcOrder(mSelectedIpqcOrder))
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGrid();
                    mSelectedIpqcOrder = null;
                }
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectedIpqcOrder == null)
            {
                MessageBox.Show("请选择一张待审核的质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIPQCApprove frm = new frmMaintainIPQCApprove(mSelectedIpqcOrder);
            frm.ThisActionType = ActionType.Approve;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
                mSelectedIpqcOrder = null;
            }
        }



        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (mSelectedIpqcOrder == null)
            {
                MessageBox.Show("请选择一张待审核的质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIPQCApprove frm = new frmMaintainIPQCApprove(mSelectedIpqcOrder);
            frm.ThisActionType = ActionType.Confirm;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
                mSelectedIpqcOrder = null;
            }
        }

        protected override void Click_Query()
        {
            BindGrid();
        }

        private void txtWorkingOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProdPlanOrderItemQuery frm = new frmMaintainProdPlanOrderItemQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrderItem != null)
            {
                txtWorkingOrder.Text = frm.SelectedProdPlanOrderItem.OrderItemCode;
                txtWorkingOrder.Tag = frm.SelectedProdPlanOrderItem;
            }
            else
            {
                txtWorkingOrder.Text = "";
                txtWorkingOrder.Tag = null;
            }
        }
        #endregion

    }
}